package com.njust.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.njust.filter.CorsFilter;
import com.njust.interceptor.LoginInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zjwpro@foxmail.com
 * @date 2018/1/20 上午10:03
 *还没有看太懂需要加强一下,做一下标记,用途及用法，以及使用场合
 */
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter{

    ////解决跨越问题ConfigurerAdapter{
    //@Override
    //public void addCorsMappings(CorsRegistry registry) {
    //    registry.addMapping("/**")
    //            .allowedOrigins("*")
    //            .allowedMethods("PUT", "DELETE","GET","POST")
    //            .allowedHeaders("*")
    //            .exposedHeaders("access-control-allow-headers",
    //                    "access-control-allow-methods",
    //                    "access-control-allow-origin",
    //                    "access-control-max-age",
    //                    "X-Frame-Options")
    //            .allowCredentials(false).maxAge(3600);
    //}

   //此方法用来专门注册一个Handler，来处理静态资源,例如图片,js,css等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * 登入拦截
     * @param registry
     * 登录拦截器,excludePathPatterns是排除访问路径?排除所有？
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //"/user/login","/user/register"
        registry.addInterceptor(new LoginInterceptor()).excludePathPatterns("/**");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

    @Bean
    public FilterRegistrationBean TestFilterRegBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CorsFilter corsFilter = new CorsFilter();
        registrationBean.setFilter(corsFilter);
        return registrationBean;
    }

}
